package com.netease.videoLoaderDemo.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.netease.neliveplayer.playerkit.videoLoader.OnVideoListTouchEventHandler;
import com.netease.videoLoaderDemo.RoomListActivity;
import com.netease.videoLoaderDemo.SpriteImageHelper;
import com.netease.videoLoaderDemo.SpriteImageInfo;
import com.netease.videoLoaderDemo.service.VideoInfo;
import com.netease.videoSlidingKit.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RoomListAdapter extends RecyclerView.Adapter<RoomListAdapter.BindingViewHolder> {
    protected final ArrayList<VideoInfo> mDataList = new ArrayList<>();
    protected final ArrayList<SpriteImageInfo> mSpriteList = new ArrayList<>();
    private RoomListActivity activity;
    public RoomListAdapter(RoomListActivity activity){
        this.activity = activity;
    }

    static class BindingViewHolder extends RecyclerView.ViewHolder {
        public AppCompatTextView mRoomId;
        public AppCompatTextView mRoomName;
        public BindingViewHolder(@NonNull View itemView) {
            super(itemView);
            mRoomId = itemView.findViewById(R.id.tvRoomId);
            mRoomName = itemView.findViewById(R.id.tvRoomName);
        }
    }

    public void insertFirst(VideoInfo item) {
        insert(0, item);
    }

    public void insertLast(VideoInfo item) {
        insert(getItemCount(), item);
    }

    public void insert(int index, VideoInfo item) {
        int itemCount = getItemCount();
        if (index < 0) {
            index = 0;
        }
        if (index > itemCount) {
            index = itemCount;
        }
        mDataList.add(index, item);
        notifyItemInserted(index);
    }

    public void replace(int index, VideoInfo item){
        mDataList.set(index, item);
        notifyItemChanged(index);
    }

    public void resetAll(List<VideoInfo> list){
        if (list == null) {
            list = new ArrayList<>();
        }
        mDataList.clear();
        mDataList.addAll(list);
        notifyDataSetChanged();
    }

    public void resetSpriteImageList(List<SpriteImageInfo> list){
        if (list == null) {
            list = new ArrayList<>();
        }
        mSpriteList.clear();
        mSpriteList.addAll(list);
        notifyDataSetChanged();
    }

    public void insertAll(List<VideoInfo> list) {
        if (list == null || list.size() <= 0) {
            return;
        }
        int itemCount = getItemCount();
        mDataList.addAll(list);
        notifyItemRangeInserted(itemCount, list.size());
    }

    public void insertAll(VideoInfo[] list) {
        if (list == null) {
            return;
        }
        int itemCount = getItemCount();
        Collections.addAll(mDataList, list);
        notifyItemRangeInserted(itemCount, list.length);
    }

    public void remove(int index) {
        int itemCount = getItemCount();
        if (index < 0 || index > itemCount) {
            return;
        }
        mDataList.remove(index);
        notifyItemRemoved(index);
    }

    public void removeAll() {
        int itemCount = getItemCount();
        if (itemCount <= 0) {
            return;
        }
        mDataList.clear();
        notifyItemRangeRemoved(0, itemCount);
    }

    public @Nullable VideoInfo getItem(int index){
        int itemCount = getItemCount();
        if (index < 0 || index >= itemCount) {
            return null;
        }
        return mDataList.get(index);
    }
    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.show_room_item, viewGroup, false);
        return new BindingViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder bindingViewHolder, int position) {
        updateVideoItem(bindingViewHolder, position);
    }

    private void updateVideoItem(@NonNull BindingViewHolder bindingViewHolder, int position) {
        VideoInfo info = (VideoInfo) getItem(position);
        bindingViewHolder.mRoomId.setText(info.vId);
        bindingViewHolder.mRoomName.setText(info.vName);
        OnVideoListTouchEventHandler handler = new OnVideoListTouchEventHandler() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        super.onTouch(v, event, position);
                        break;
                    case MotionEvent.ACTION_UP:
                        super.onTouch(v, event, position);
                        activity.startVideoItemActivity(mDataList, mSpriteList, position);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        super.onTouch(v, event, position);
                        break;
                    // 其他事件类型的处理
                }
                return true;
            }
        };
        bindingViewHolder.itemView.setOnTouchListener(handler);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


}
